package org.bigdata.framework.admin.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bigdata.framework.admin.web.util.DateUtils;
import org.bigdata.framework.core.model.AnalysisTopic;
import org.bigdata.framework.core.model.AnalysisTopicPermission;
import org.bigdata.framework.core.model.AnalysisTopicTarget;
import org.bigdata.framework.core.model.Classes;
import org.bigdata.framework.core.model.CommonDataProperty;
import org.bigdata.framework.core.model.DataProperty;
import org.bigdata.framework.core.model.Member;
import org.bigdata.framework.core.protocol.IAnalysisService;
import org.bigdata.framework.core.protocol.IDynamicService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.bigdata.framework.core.protocol.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.rpc.Result;

/**
 * 专题对应Controller
* @ClassName: AnalysisController  
* @Description: TODO
* @author xuweiheng 
* @date 2017-7-23  
*
 */

@Controller
public class AnalysisController extends BaseContoroller{
	@Autowired 
	private IAnalysisService analysisService;
	@Autowired
	private IPublicService publicService;
	@Autowired
	private IDynamicService dynamicService;
	@Autowired
	private ISearchService searchService;
	/**
	 * 跳转专题管理界面
	* @Title: analysisSpecialManageList  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws 	
	 */
	@RequestMapping("analysis-special-manage-list.html")
	public String analysisSpecialManageList() {
		return "/modules/analysis/analysis_special_manage_list";
	}
	/**
	 * 跳转新增界面
	* @Title: analysisSpecialAddUpdate  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("analysis_special_add_update.html")
	public String analysisSpecialAddUpdate() {
		return "/modules/analysis/analysis_special_add_update";
	}
	
	/**
	 * 跳转至 知识库选择界面
	* @Title: analysisClassesRecommendClassZTree  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("analysis-Classes-recommendClass-zTree.html")
	public String analysisClassesRecommendClassZTree() {
		return "/modules/analysis/analysis_Classes_recommendClass_zTree";
	}
	
	/**
	 *  跳转筛选条件页面
	* @Title: analysisConditionRecommendClassZTree  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws   
	 */
	@RequestMapping("analysis-condition-recommendClass-zTree.html")
	public String analysisConditionRecommendClassZTree() {
		return "/modules/analysis/analysis_condition_recommendClass_zTree";
	}
	/**
	 *  跳转分析指标页面
	* @Title: analysisConditionRecommendClassZTree  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws    analysis-jurisdiction-configuration-list.html
	 */
	@RequestMapping("analysis-analyze-index-add-update.html")
	public String analysisAnalyzeIndexAddUpdate() {
		return "/modules/analysis/analysis_analyze_index_add_update";
	}
	
	/**
	 *  跳转专题权限配置页面
	* @Title: analysisJurisdictionConfigurationList  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("analysis-jurisdiction-configuration-list.html")
	public String analysisJurisdictionConfigurationList() {
		return "/modules/analysis/analysis_jurisdiction_configuration_list";
	}
	
	/**
	 *  跳转专题权限配置添加修改页面
	* @Title: analysisJurisdictionConfigurationAddUpdate  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  analysis-jurisdiction-configuration-user.html
	 */
	@RequestMapping("analysis-jurisdiction-configuration-add-update.html")
	public String analysisJurisdictionConfigurationAddUpdate() {
		return "/modules/analysis/analysis_jurisdiction_configuration_add_update";
	}
	/**
	 *   跳转专题权限配置添加修改 -选择用户页面
	* @Title: analysisJurisdictionConfigurationUser  
	* @Description: TODO 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("analysis-jurisdiction-configuration-user.html")
	public String analysisJurisdictionConfigurationUser() {
		return "/modules/analysis/analysis_jurisdiction_configuration_user";
	}
	/**
	 * 	查询字段项的方法
	* @Title: toAnalysisclassListall
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisclassListall")
	public void toAnalysisclassListall(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String anId = request.getParameter("anId");
		String[] an = anId.split(";");
		List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
		LinkedHashMap<String, Object> mp = null;
		if(an.length>1){
			List<CommonDataProperty> dataProperty = publicService.getCommonDataProperty();
			for (int i = 0; i < dataProperty.size(); i++) {
				CommonDataProperty cdp = dataProperty.get(i);
				mp = new LinkedHashMap<String, Object>();
				mp.put("id", cdp.getId());
				mp.put("nameZh", cdp.getNameZh());
				mp.put("nameEn", cdp.getNameEn());
				mp.put("name", cdp.getNameZh() + "(" + cdp.getNameEn()+ ")");
				r.add(mp);
			}
		}else{
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,name_en", an[0] );
			List<Classes> classes = publicService.getClasses(queryParams, 0, 2);
			List<DataProperty> dataProperty1 = publicService.getDataProperty(classes.get(0));
			
			for (int i = 0; i < dataProperty1.size(); i++) {
				DataProperty dp = dataProperty1.get(i);
				mp = new LinkedHashMap<String, Object>();
				mp.put("id", dp.getId());
				mp.put("nameZh", dp.getNameZh());
				mp.put("nameEn", dp.getNameEn());
				mp.put("name", dp.getNameZh() + "(" + dp.getNameEn()+ ")");
				r.add(mp);
			}
		}
		String result = formatToJson(r);
		/*System.out.println(result);*/
		super.pringWriterToPage(result.toString(), "application/json", response);
	}
	
	/**
	 * 	添加图表的方法
	* @Title: toAnalysisSaveChart  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws  
	 */
	@RequestMapping("analysisSaveChart")
	public void toAnalysisSaveChart(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			String pId = request.getParameter("pId");
			String analysisChartName = request.getParameter("analysisChartName");
			String analyzeEnName = request.getParameter("analyzeEnName");
			String stauts = request.getParameter("stauts");
			String chartGraph = request.getParameter("chartGraph");
			String repositoryId = request.getParameter("repositoryId");
			AnalysisTopicTarget analysisTopicTarget = new AnalysisTopicTarget();
			analysisTopicTarget.setTargetName(analysisChartName);
			analysisTopicTarget.setTargetField(analyzeEnName);
			analysisTopicTarget.setChartType(chartGraph);
			analysisTopicTarget.setNumberStatistic(Integer.parseInt(stauts));
			if(!"0".equals(pId)){
				analysisTopicTarget.setAnalysisTopicId(Integer.parseInt(pId));
			}
//			AnalysisTopicTarget saveAnalysisTopicTarget = null;
			/*if(id.equals("0")){*/
			AnalysisTopicTarget saveAnalysisTopicTarget = analysisService.saveAnalysisTopicTarget(analysisTopicTarget);
			/*}else{
				analysisTopicTarget.setAnalysisTopicId(Integer.parseInt(id));
				saveAnalysisTopicTarget = analysisService.saveAnalysisTopicTarget(analysisTopicTarget);
			}*/
			String rep[] = repositoryId.split(";");
			String analyzeZhName = "";
			String analyzeStauts = "";
			String analyzeChart = "";
			if(rep.length>1){
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,name_en", analyzeEnName);
				List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParams, 0, 2);
				analyzeZhName = commonDataProperty.get(0).getNameZh();
			}else{
				HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
				queryParamsClass.put("=,name_en", repositoryId);
				List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,classes_id", classes.get(0).getId());
				queryParams.put("=,name_en", analyzeEnName);
				List<DataProperty> dataProperty = publicService.getDataProperty(queryParams, 0, 2);
				analyzeZhName = dataProperty.get(0).getNameZh();
			}
			if(stauts.equals("0")){
				analyzeStauts = "是";
			}else if(stauts.equals("1")){
				analyzeStauts = "否";
			}
			if(chartGraph.equals("broken")){
				analyzeChart ="折线图";
			}
			if(chartGraph.equals("pie")){
				analyzeChart ="饼图";
			}
			if(chartGraph.equals("bar")){
				analyzeChart ="柱状图";
			}
			if(chartGraph.equals("circular")){
				analyzeChart ="关系图";
			}
			if(chartGraph.equals("force")){
				analyzeChart ="力导向图";
			}
			if(chartGraph.equals("funnel")){
				analyzeChart ="漏斗图";
			}
			if(chartGraph.equals("radar")){
				analyzeChart ="雷达图";
			}
			if(chartGraph.equals("tendency")){
				analyzeChart ="趋势图";
			}
			HashMap<String, Object> analyzeMap = new HashMap<String, Object>();
			analyzeMap.put("analyzeZhName", analyzeZhName);
			analyzeMap.put("analyzeChartZh", analyzeChart);
			analyzeMap.put("analyzeChartEn", chartGraph);
			analyzeMap.put("analyzeStauts", analyzeStauts);
			analyzeMap.put("analysisChartName", analysisChartName);
			analyzeMap.put("analysisId", saveAnalysisTopicTarget.getId());
			List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
			resultList.add(analyzeMap);
			result.put("msg", "OK");
			result.put("data", resultList);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("msg", "NO");
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
		
	}
	
	/**
	 * 		专题管理添加
	* @Title: toAnalysisSaveList  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisSaveList")
	public void toAnalysisSaveList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			//中文名称
			String analysisCnname = request.getParameter("analysisCnname");
			//英文名称
			String analysisEnname = request.getParameter("analysisEnname");
			//图片
			String avatarImg = request.getParameter("avatarImg");
			//排序
			String analysisSort = request.getParameter("analysisSort");
			//简介
			String analysisSynopsis = request.getParameter("analysisSynopsis");
			//选择知识库
			String libraryEnChoose = request.getParameter("libraryEnChoose");
			//选择字段项
			String parentClassEnname = request.getParameter("parentClassEnname");
			//是否开启对比分析
			String analysisStauts = request.getParameter("analysisStauts");
			//分析指标
			String chartId = request.getParameter("chartId");
			//传过来的id
			String id = request.getParameter("id");
			String createTime = DateUtils.getDateTime();
			AnalysisTopic analysisTopic = new AnalysisTopic();
			analysisTopic.setNameZh(analysisCnname);
			analysisTopic.setNameEn(analysisEnname);
			analysisTopic.setCover(avatarImg);
			analysisTopic.setSort(Integer.parseInt(analysisSort));
			analysisTopic.setDescription(analysisSynopsis);
			analysisTopic.setClassesEn(libraryEnChoose);
			analysisTopic.setConditionFields(parentClassEnname);
			analysisTopic.setConditionCompare(Integer.parseInt(analysisStauts));
			analysisTopic.setCreateTime(createTime);
			if("0".equals(id)){
				AnalysisTopic saveAnalysisTopic = analysisService.saveAnalysisTopic(analysisTopic);
				String[] ch = chartId.split(";");
				for (int i = 0; i < ch.length; i++) {
					AnalysisTopicTarget analysisTopicTarget = analysisService.findAnalysisTopicTarget(Integer.parseInt(ch[i]));
					analysisTopicTarget.setAnalysisTopicId(saveAnalysisTopic.getId());
					analysisService.editAnalysisTopicTarget(analysisTopicTarget);
				}
			}else if(!"0".equals(id)){
				analysisTopic.setId(Integer.parseInt(id));
				AnalysisTopic editAnalysisTopic = analysisService.editAnalysisTopic(analysisTopic);
				HashMap<String, Object> queryParams = new HashMap<String, Object>();
				queryParams.put("=,analysis_topic_id", id);
				List<AnalysisTopicTarget> analysisTopicTarget = analysisService.getAnalysisTopicTarget(queryParams, 0, 100);
				String[] ch = chartId.split(";");
				for (int i = 0; i < ch.length; i++) {
					for (int j = 0; j < analysisTopicTarget.size(); j++) {
						if(!ch[i].equals(analysisTopicTarget.get(j).getId()+"")){
							AnalysisTopicTarget analysisTopicTarget1 = analysisService.findAnalysisTopicTarget(Integer.parseInt(ch[i]));
							analysisTopicTarget1.setAnalysisTopicId(editAnalysisTopic.getId());
							analysisService.editAnalysisTopicTarget(analysisTopicTarget1);
						}
					}
				}
			}
			result.put("msg", "OK");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("msg", "NO");
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	/**
	 * 	展示全部方法
	* @Title: toAnalysisFindAll  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws  text
	 */
	@RequestMapping("analysisFindAll")
	public void toAnalysisFindAll(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String page = request.getParameter("page");
			String queryType = request.getParameter("queryParams");
			String queryText = request.getParameter("text");
			HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
			queryParams.put("^,sort", "asc");
			if(queryText != null && !"".equals(queryText)){
				queryParams.put("%,"+queryType, queryText);
			}
			List<AnalysisTopic> analysisTopic = analysisService.getAnalysisTopic(queryParams, Integer.parseInt(page), 100);
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			Map<String,Object> anResult = null;
			for (int i = 0; i < analysisTopic.size(); i++) {
				anResult = new HashMap<String, Object>();
				StringBuffer classesZh = new StringBuffer();
				StringBuffer conditionFieldsZh = new StringBuffer();
				String analysisStauts = "";
				String[] SClass = analysisTopic.get(i).getClassesEn().split(";");
				String[] cfZh = analysisTopic.get(i).getConditionFields().split(";");
				for (int j = 0; j < SClass.length; j++) {
					HashMap<String, Object> queryParamsClasses = new HashMap<String, Object>();
					queryParamsClasses.put("=,name_en",SClass[j]);
					List<Classes> classes = publicService.getClasses(queryParamsClasses, 0, 10);
					classesZh.append("<label><button type=\"button\" class=\"btn btn-sm btn-info\">");
					classesZh.append(classes.get(0).getNameZh());
					classesZh.append("</button></label>&nbsp;&nbsp;");
				}
				if(SClass.length >1){
					for (int k = 0; k < cfZh.length; k++) {
						HashMap<String, Object> queryParamsc = new HashMap<String, Object>();
						queryParamsc.put("=,name_en", cfZh[k]);
						List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParamsc, 0, 2);
						conditionFieldsZh.append("<label><button type=\"button\" class=\"btn btn-sm btn-info\">");
						conditionFieldsZh.append(commonDataProperty.get(0).getNameZh());
						conditionFieldsZh.append("</button></label>&nbsp;&nbsp;");
					}
				}else{
					for (int n = 0; n < cfZh.length; n++) {
						HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
						queryParamsClass.put("=,name_en", SClass[0]);
						List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
						HashMap<String, Object> queryParamsd = new HashMap<String, Object>();
						queryParamsd.put("=,classes_id", classes.get(0).getId());
						queryParamsd.put("=,name_en", cfZh[n]);
						List<DataProperty> dataProperty = publicService.getDataProperty(queryParamsd, 0, 2);
						conditionFieldsZh.append("<label><button type=\"button\" class=\"btn btn-sm btn-info\">");
						conditionFieldsZh.append(dataProperty.get(0).getNameZh());
						conditionFieldsZh.append("</button></label>&nbsp;&nbsp;");
					}
				}
				if(analysisTopic.get(i).getConditionCompare()==0){
					analysisStauts = "是";
				}
				if(analysisTopic.get(i).getConditionCompare()==1){
					analysisStauts = "否";
				}
				anResult.put("id", analysisTopic.get(i).getId());
				anResult.put("nameZh", analysisTopic.get(i).getNameZh());
				anResult.put("nameEn", analysisTopic.get(i).getNameEn());
				anResult.put("sort", analysisTopic.get(i).getSort());
				anResult.put("classesZh", classesZh.toString());
				anResult.put("conditionFieldsZh", conditionFieldsZh.toString());
				anResult.put("analysisStauts", analysisStauts);
				resultList.add(anResult);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
			
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 * 	根据id删除专题管理的内容
	* @Title: toAnalysisDel  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisDel")
	public void toAnalysisDel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			HashMap<String, Object> queryParamsIds = new LinkedHashMap<String, Object>();
			Integer count = analysisService.getAnalysisTopicPermissionCount(queryParamsIds);
			List<AnalysisTopicPermission> analysisTopicPermission = analysisService.getAnalysisTopicPermission(queryParamsIds, 0, count);
			String delete = "";
			for (int j = 0; j < analysisTopicPermission.size(); j++) {
				String[] split = analysisTopicPermission.get(j).getAnalysisTopicIds().split(";");
				for (int n = 0; n < split.length; n++) {
					if(id.equals(split[n])){
						delete = "no";
						break;
					}
				}
				if("no".equals(delete)){
					break;
				}
			}
			if("".equals(delete)){
				HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
				queryParams.put("=,analysis_topic_id", id);
				List<AnalysisTopicTarget> analysisTopicTarget = analysisService.getAnalysisTopicTarget(queryParams, 0, 100);
				for (int i = 0; i < analysisTopicTarget.size(); i++) {
					analysisService.deleteAnalysisTopicTarget(analysisTopicTarget.get(i));
				}
				AnalysisTopic analysisTopic = analysisService.findAnalysisTopic(Integer.parseInt(id));
				analysisService.deleteAnalysisTopic(analysisTopic);
				result.put("msg", "OK");
			}else if("no".equals(delete)){
				result.put("msg", "NOT");
			}
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 *  加载编辑专题的上部内容
	* @Title: findTopUpdate  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisUpdate")
	public void toAnalysisUpdate(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			AnalysisTopic analysisTopic = analysisService.findAnalysisTopic(Integer.parseInt(id));
			StringBuffer classesZh = new StringBuffer();
			StringBuffer conditionFieldsZh = new StringBuffer();
			String analysisStauts = "";
			List<Map<String,Object>> chartList = new ArrayList<Map<String,Object>>();
			Map<String,Object> chartMap = null;
			Map<String,Object> resultMap = new HashMap<String,Object>();
			String[] SClass = analysisTopic.getClassesEn().split(";");
			String[] cfZh = analysisTopic.getConditionFields().split(";");
			for (int j = 0; j < SClass.length; j++) {
				HashMap<String, Object> queryParamsClassed = new HashMap<String, Object>();
				queryParamsClassed.put("=,name_en", SClass[j]);
				List<Classes> classes = publicService.getClasses(queryParamsClassed, 0, 2);
				/*Classes classes = publicService.findClasses(Integer.parseInt(SClass[j]));*/
				classesZh.append(classes.get(0).getNameZh());
				classesZh.append(";");
			}
			if(SClass.length >1){
				for (int k = 0; k < cfZh.length; k++) {
					HashMap<String, Object> queryParamsc = new HashMap<String, Object>();
					queryParamsc.put("=,name_en", cfZh[k]);
					List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParamsc, 0, 2);
					conditionFieldsZh.append(commonDataProperty.get(0).getNameZh());
					conditionFieldsZh.append(";");
				}
			}else{
				for (int n = 0; n < cfZh.length; n++) {
					HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
					queryParamsClass.put("=,name_en", SClass[0]);
					List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
					HashMap<String, Object> queryParamsd = new HashMap<String, Object>();
					queryParamsd.put("=,classes_id", classes.get(0).getId());
					queryParamsd.put("=,name_en", cfZh[n]);
					List<DataProperty> dataProperty = publicService.getDataProperty(queryParamsd, 0, 2);
					conditionFieldsZh.append(dataProperty.get(0).getNameZh());
					conditionFieldsZh.append(";");
				}
			}
			/*if(analysisTopic.getConditionCompare()==0){
				analysisStauts = "是";
			}
			if(analysisTopic.getConditionCompare()==1){
				analysisStauts = "否";
			}*/
			HashMap<String, Object> queryParams = new LinkedHashMap<String, Object>();
			queryParams.put("=,analysis_topic_id", id);
			List<AnalysisTopicTarget> analysisTopicTarget = analysisService.getAnalysisTopicTarget(queryParams, 0, 100);
			for (int i = 0; i < analysisTopicTarget.size(); i++) {
				chartMap = new HashMap<String,Object>();
				String rep[] = analysisTopic.getClassesEn().split(";");
				String analyzeZhName = "";
				String analyzeStauts = "";
				String analyzeChart = "";
				if(rep.length>1){
					HashMap<String, Object> queryParamsc = new HashMap<String, Object>();
					queryParamsc.put("=,name_en", analysisTopicTarget.get(i).getTargetField());
					List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParamsc, 0, 2);
					analyzeZhName = commonDataProperty.get(0).getNameZh();
				}else{
					HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
					queryParamsClass.put("=,name_en", rep[0]);
					List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
					HashMap<String, Object> queryParamsd = new HashMap<String, Object>();
					queryParamsd.put("=,classes_id", classes.get(0).getId());
					queryParamsd.put("=,name_en", analysisTopicTarget.get(i).getTargetField());
					List<DataProperty> dataProperty = publicService.getDataProperty(queryParamsd, 0, 2);
					analyzeZhName = dataProperty.get(0).getNameZh();
				}
				if(analysisTopicTarget.get(i).getNumberStatistic()==0){
					analyzeStauts = "是";
				}else if(analysisTopicTarget.get(i).getNumberStatistic()==1){
					analyzeStauts = "否";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("broken")){
					analyzeChart ="折线图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("pie")){
					analyzeChart ="饼图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("bar")){
					analyzeChart ="柱状图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("circular")){
					analyzeChart ="关系图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("force")){
					analyzeChart ="力导向图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("funnel")){
					analyzeChart ="漏斗图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("radar")){
					analyzeChart ="雷达图";
				}
				if(analysisTopicTarget.get(i).getChartType().equals("tendency")){
					analyzeChart ="趋势图";
				}
				chartMap.put("id", analysisTopicTarget.get(i).getId());
				chartMap.put("chartName", analysisTopicTarget.get(i).getTargetName());
				chartMap.put("analyzeChart", analyzeChart);
				chartMap.put("analyzeStauts", analyzeStauts);
				chartMap.put("analyzeZhName", analyzeZhName);
				chartMap.put("chartType", analysisTopicTarget.get(i).getChartType());
				chartList.add(chartMap);
			}
			resultMap.put("nameZh",analysisTopic.getNameZh());
			resultMap.put("nameEn",analysisTopic.getNameEn());
			resultMap.put("imgSrc",analysisTopic.getCover());
			resultMap.put("sort",analysisTopic.getSort());
			resultMap.put("description",analysisTopic.getDescription());
			resultMap.put("classesEn",analysisTopic.getClassesEn());
			resultMap.put("classesZh",classesZh.toString());
			resultMap.put("conditionFieldsEn",analysisTopic.getConditionFields());
			resultMap.put("conditionFieldsZh",conditionFieldsZh.toString());
			resultMap.put("compare",analysisTopic.getConditionCompare());
			resultMap.put("chart",chartList);
			result.put("msg", "OK");
			result.put("data", resultMap);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 * 	修改分析指标
	* @Title: toAnalysisUpdateChart  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	
	@RequestMapping("analysisUpdateChart")
	public void toAnalysisUpdateChart(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			String pId = request.getParameter("pId");
			String analysisChartName = request.getParameter("analysisChartName");
			String analyzeEnName = request.getParameter("analyzeEnName");
			String stauts = request.getParameter("stauts");
			String chartGraph = request.getParameter("chartGraph");
			String repositoryId = request.getParameter("repositoryId");
			String chartId = request.getParameter("chartId");
			AnalysisTopicTarget analysisTopicTarget = new AnalysisTopicTarget();
			analysisTopicTarget.setTargetName(analysisChartName);
			analysisTopicTarget.setTargetField(analyzeEnName);
			analysisTopicTarget.setChartType(chartGraph);
			analysisTopicTarget.setNumberStatistic(Integer.parseInt(stauts));
			analysisTopicTarget.setId(Integer.parseInt(id));
			if(!"0".equals(pId)){
				analysisTopicTarget.setAnalysisTopicId(Integer.parseInt(pId));
			}
			analysisService.editAnalysisTopicTarget(analysisTopicTarget);
			String[] cid = chartId.split(";");
			String[] rep = repositoryId.split(";");
			Map<String, Object> attMap = null;
			List<Map<String, Object>> attList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < cid.length; i++) {
				attMap = new HashMap<String, Object>();
				AnalysisTopicTarget findAnalysisTopicTarget = analysisService.findAnalysisTopicTarget(Integer.parseInt(cid[i]));
				String analyzeZhName = "";
				String analyzeStauts = "";
				String analyzeChart = "";
				String stautsString = findAnalysisTopicTarget.getNumberStatistic()+"";
				if(rep.length>1){
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,name_en", findAnalysisTopicTarget.getTargetField());
					List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParams, 0, 2);
					analyzeZhName = commonDataProperty.get(0).getNameZh();
				}else{
					HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
					queryParamsClass.put("=,name_en", repositoryId);
					List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
					HashMap<String, Object> queryParams = new HashMap<String, Object>();
					queryParams.put("=,classes_id", classes.get(0).getId());
					queryParams.put("=,name_en", findAnalysisTopicTarget.getTargetField());
					List<DataProperty> dataProperty = publicService.getDataProperty(queryParams, 0, 2);
					analyzeZhName = dataProperty.get(0).getNameZh();
				}
				if(stautsString.equals("0")){
					analyzeStauts = "是";
				}else if(stautsString.equals("1")){
					analyzeStauts = "否";
				}
				if(findAnalysisTopicTarget.getChartType().equals("broken")){
					analyzeChart ="折线图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("pie")){
					analyzeChart ="饼图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("bar")){
					analyzeChart ="柱状图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("circular")){
					analyzeChart ="关系图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("force")){
					analyzeChart ="力导向图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("funnel")){
					analyzeChart ="漏斗图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("radar")){
					analyzeChart ="雷达图";
				}
				if(findAnalysisTopicTarget.getChartType().equals("tendency")){
					analyzeChart ="趋势图";
				}
				attMap.put("analysisId", findAnalysisTopicTarget.getId());
				attMap.put("analyzeZhName", analyzeZhName);
				attMap.put("chartName", findAnalysisTopicTarget.getTargetName());
				attMap.put("analyzeStauts", analyzeStauts);
				attMap.put("analyzeChart", analyzeChart);
				attMap.put("chartType", findAnalysisTopicTarget.getChartType());
				attList.add(attMap);
			}
			result.put("msg", "OK");
			result.put("data", attList);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
		
	}
	
	
	/**
	 * 	显示分析指标内容
	* @Title: toChartUpdateShow  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("chartUpdateShow")
	public void toChartUpdateShow(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			String repositoryId = request.getParameter("repositoryId");
			AnalysisTopicTarget analysisTopicTarget = analysisService.findAnalysisTopicTarget(Integer.parseInt(id));
			String[] repd = repositoryId.split(";");
			String analyzeZhName = "";
			Map<String,Object> chartMap = new HashMap<String, Object>();
			if(repd.length>1){
				HashMap<String, Object> queryParamsc = new HashMap<String, Object>();
				queryParamsc.put("=,name_en", analysisTopicTarget.getTargetField());
				List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParamsc, 0, 2);
				analyzeZhName = commonDataProperty.get(0).getNameZh();
			}else{
				HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
				queryParamsClass.put("=,name_en", repd[0]);
				List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
				HashMap<String, Object> queryParamsd = new HashMap<String, Object>();
				queryParamsd.put("=,classes_id", classes.get(0).getId());
				queryParamsd.put("=,name_en", analysisTopicTarget.getTargetField());
				List<DataProperty> dataProperty = publicService.getDataProperty(queryParamsd, 0, 2);
				analyzeZhName = dataProperty.get(0).getNameZh();
			}
			chartMap.put("id", analysisTopicTarget.getId());
			chartMap.put("chartName", analysisTopicTarget.getTargetName());
			chartMap.put("analyzeEnName", analysisTopicTarget.getTargetField());
			chartMap.put("analyzeZhName", analyzeZhName);
			chartMap.put("chartType", analysisTopicTarget.getChartType());
			chartMap.put("stauts", analysisTopicTarget.getNumberStatistic());
			result.put("msg", "OK");
			result.put("data", chartMap);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 * 	删除分析指标
	* @Title: toAnalysisDelChart  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisDelChart")
	public void toAnalysisDelChart(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			String chartId = request.getParameter("chartId");
			String libraryEnChoose = request.getParameter("libraryEnChoose");
			AnalysisTopicTarget analysisTopicTarget = analysisService.findAnalysisTopicTarget(Integer.parseInt(id));
			analysisService.deleteAnalysisTopicTarget(analysisTopicTarget);
			String[] chart = chartId.split(";");
			StringBuffer chartResult = new StringBuffer();
			for (int i = 0; i < chart.length; i++) {
				if(!id.equals(chart[i])){
					chartResult.append(chart[i]);
					chartResult.append(";");
				}
			}
			String crt = chartResult.toString();
			List<Map<String, Object>> attList = new ArrayList<Map<String, Object>>();
			if(!"".equals(crt)){
				String[] cr = chartResult.toString().split(";");
				Map<String, Object> attMap = null;
				String[] rep = libraryEnChoose.split(";");
				for (int i = 0; i < cr.length; i++) {
					attMap = new HashMap<String, Object>();
					AnalysisTopicTarget findAnalysisTopicTarget = analysisService.findAnalysisTopicTarget(Integer.parseInt(cr[i]));
					String analyzeZhName = "";
					String analyzeStauts = "";
					String analyzeChart = "";
					String chartStauts = findAnalysisTopicTarget.getNumberStatistic()+"";
					if(rep.length>1){
						HashMap<String, Object> queryParams = new HashMap<String, Object>();
						queryParams.put("=,name_en", findAnalysisTopicTarget.getTargetField());
						List<CommonDataProperty> commonDataProperty = publicService.getCommonDataProperty(queryParams, 0, 2);
						analyzeZhName = commonDataProperty.get(0).getNameZh();
					}else{
						HashMap<String, Object> queryParamsClass = new HashMap<String, Object>();
						queryParamsClass.put("=,name_en", rep[0]);
						List<Classes> classes = publicService.getClasses(queryParamsClass, 0, 1);
						HashMap<String, Object> queryParams = new HashMap<String, Object>();
						queryParams.put("=,classes_id", classes.get(0).getId());
						queryParams.put("=,name_en", findAnalysisTopicTarget.getTargetField());
						List<DataProperty> dataProperty = publicService.getDataProperty(queryParams, 0, 2);
						analyzeZhName = dataProperty.get(0).getNameZh();
					}
					if(chartStauts.equals("0")){
						analyzeStauts = "是";
					}else if(chartStauts.equals("1")){
						analyzeStauts = "否";
					}
					if(findAnalysisTopicTarget.getChartType().equals("broken")){
						analyzeChart ="折线图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("pie")){
						analyzeChart ="饼图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("bar")){
						analyzeChart ="柱状图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("circular")){
						analyzeChart ="关系图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("force")){
						analyzeChart ="力导向图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("funnel")){
						analyzeChart ="漏斗图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("radar")){
						analyzeChart ="雷达图";
					}
					if(findAnalysisTopicTarget.getChartType().equals("tendency")){
						analyzeChart ="趋势图";
					}
					attMap.put("id", findAnalysisTopicTarget.getId());
					attMap.put("analyzeZhName", analyzeZhName);
					attMap.put("chartName", findAnalysisTopicTarget.getTargetName());
					attMap.put("analyzeStauts", analyzeStauts);
					attMap.put("analyzeChart", analyzeChart);
					attMap.put("chartType", findAnalysisTopicTarget.getChartType());
					attList.add(attMap);
				}
			}
			result.put("msg", "OK");
			result.put("data", attList);
			result.put("chartId", chartResult.toString());
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 *  加载全部专题
	* @Title: toAnalysisAll  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("analysisAll")
	public void toAnalysisAll(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			List<AnalysisTopic> analysisTopic = analysisService.getAnalysisTopic();
			Map<String,Object> resultMap = null;
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			for (int i = 0; i < analysisTopic.size(); i++) {
				resultMap = new HashMap<String,Object>();
				resultMap.put("id", analysisTopic.get(i).getId());
				resultMap.put("nameZh", analysisTopic.get(i).getNameZh());
				resultList.add(resultMap);
			}
			result.put("msg", "OK");
			result.put("data", resultList);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 *  专题权限配置添加
	* @Title: toSaveJurisdiction  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws  
	 */
	@RequestMapping("saveJurisdiction")
	public void toSaveJurisdiction(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			String organizationUserid = request.getParameter("organizationUserid");
			String special = request.getParameter("special");
			Member member = publicService.findMember(Integer.parseInt(organizationUserid));
			String createTime = DateUtils.getDateTime();
			int memberType = 1;
			if(member.getType() == 0 || member.getType() == 2){
				
				memberType = 0;
			}
			if(member.getType() == 1){
				memberType = 1;
			}
			AnalysisTopicPermission analysisTopicPermission = new AnalysisTopicPermission();
			analysisTopicPermission.setAnalysisTopicIds(special);
			analysisTopicPermission.setCreateTime(createTime);
			analysisTopicPermission.setMemberType(memberType);
			analysisTopicPermission.setMemberUsername(member.getUsername());
			if("0".equals(id)){
				analysisService.saveAnalysisTopicPermission(analysisTopicPermission);
			}else{
				analysisTopicPermission.setId(Integer.parseInt(id));
				analysisService.editAnalysisTopicPermission(analysisTopicPermission);
			}
			result.put("msg", "OK");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
		
	}
	
	/**
	 *  专题权限配置查询全部
	* @Title: toQueryAnalysisAll  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws 
	 */
	@RequestMapping("queryJurisdictionAll")
	public void toQueryAnalysisAll(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String page = request.getParameter("page");
			String username = request.getParameter("username");
			String realname = request.getParameter("realname");
			String type = request.getParameter("type");
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			if(username != null && !"".equals(username)){
				queryParams.put("%,username", username);
			}
			if(realname != null && !"".equals(realname)){
				queryParams.put("%,realname", realname);
			}
			if(type != null && !"".equals(type)){
				queryParams.put("=,type", type);
			}
			List<Member> memberList=publicService.getMember(queryParams, Integer.parseInt(page), 100);
//			List<AnalysisTopicPermission> analysisTopicPermission = analysisService.getAnalysisTopicPermission(queryParams, Integer.parseInt(page), 15);
			Map<String,Object> resultMap = null;
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			for (int i = 0; i < memberList.size(); i++) {
				HashMap<String, Object> memberQueryParams = new HashMap<String, Object>();
				memberQueryParams.put("=,member_username", memberList.get(i).getUsername());
				List<AnalysisTopicPermission> analysisTopicPermission = analysisService.getAnalysisTopicPermission(memberQueryParams, 1, 15);
				for (int n = 0; n < analysisTopicPermission.size(); n++) {
					resultMap = new HashMap<String,Object>();
					StringBuffer sb = new StringBuffer();
					String sType = "";
					HashMap<String, Object> queryParamsm = new HashMap<String, Object>();
					queryParamsm.put("=,username", analysisTopicPermission.get(n).getMemberUsername());
					List<Member> member = publicService.getMember(queryParamsm, 0, 2);
					String[] an = analysisTopicPermission.get(n).getAnalysisTopicIds().split(";");
					for (int j = 0; j < an.length; j++) {
						AnalysisTopic findAnalysisTopic = analysisService.findAnalysisTopic(Integer.parseInt(an[j]));
						sb.append("<button type=\"button\" class=\"btn btn-sm btn-info\" >");
						sb.append(findAnalysisTopic.getNameZh());
						sb.append("</button>&nbsp;&nbsp;");
					}
					if(analysisTopicPermission.get(n).getMemberType() ==0){
						sType ="个人用户";
					}
					if(analysisTopicPermission.get(n).getMemberType() ==1){
						sType ="机构用户";
					}
					resultMap.put("realName", member.get(0).getRealname());
					resultMap.put("username", member.get(0).getUsername());
					resultMap.put("sType", sType);
					resultMap.put("specialName", sb.toString());
					resultMap.put("id", analysisTopicPermission.get(n).getId());
					resultList.add(resultMap);
				}
			}
			result.put("msg", "OK");
			result.put("data", resultList);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 *  删除专题权限配置
	* @Title: toJurisdictionDelete  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws  
	 */
	@RequestMapping("jurisdictionDelete")
	public void toJurisdictionDelete(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			AnalysisTopicPermission analysisTopicPermission = analysisService.findAnalysisTopicPermission(id);
			analysisService.deleteAnalysisTopicPermission(analysisTopicPermission);
			result.put("msg", "OK");
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
	
	/**
	 *  根据id查询对应的权限
	* @Title: toJurisdictionQueryId  
	* @Description: TODO 
	* @param @param request
	* @param @param response
	* @param @throws Exception    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("jurisdictionQueryId")
	public void toJurisdictionQueryId(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			AnalysisTopicPermission analysisTopicPermission = analysisService.findAnalysisTopicPermission(id);
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
			queryParams.put("=,username", analysisTopicPermission.getMemberUsername());
			List<Member> member = publicService.getMember(queryParams, 0, 1);
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("memberName", member.get(0).getUsername());
			resultMap.put("memberId", member.get(0).getId());
			resultMap.put("specialId", analysisTopicPermission.getAnalysisTopicIds());
			result.put("msg", "OK");
			result.put("data", resultMap);
		} catch (Exception e) {
			result.put("msg", "NO");
			e.printStackTrace();
		}
		String resultString = formatToJson(result);
//		System.out.println(resultString);
		super.pringWriterToPage(resultString, "application/json", response);
	}
}
